package com.itheima.inetaddress;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Thread implements Runnable{
    private Socket Threadaccept;

    public Thread(Socket accept) {
        this.Threadaccept = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            //创建本地流,用于存数据
            bos = new BufferedOutputStream(new FileOutputStream("test-inetaddress\\service\\"+ UUID.randomUUID().toString() + ".png"));
            //创建网络中的流,用于读数据
            InputStream is = Threadaccept.getInputStream();
            //包装一下
            BufferedInputStream bis = new BufferedInputStream(is);

            //将网络中的数据写到本地流中
            int len;
            while((len = bis.read()) != -1){
                bos.write(len);
            }

            //给出反馈,用于写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Threadaccept.getOutputStream()));
            bw.write("上传成功");
            //换行
            bw.newLine();
            //刷新
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                Threadaccept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
